package com.windoent.yugioh.ui.activity.saishi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.text.Html;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.viewpager.widget.ViewPager;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.MyApplication;
import com.windoent.yugioh.R;
import com.windoent.yugioh.adapter.GameManagerFragmentViewPagerAdapter;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.OnGoingDuiZhenBean;
import com.windoent.yugioh.bean.SaiShiDetailsBean;
import com.windoent.yugioh.bean.eventbus.FragmentActivityMessageEvent;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.help.mengceng.NewbieGuide;
import com.windoent.yugioh.help.mengceng.core.Controller;
import com.windoent.yugioh.help.mengceng.listener.OnGuideChangedListener;
import com.windoent.yugioh.help.mengceng.model.GuidePage;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.ui.activity.meinfo.MyMsgActivity;
import com.windoent.yugioh.ui.fragment.saishi.end.EndedDetailsFragment;
import com.windoent.yugioh.ui.fragment.saishi.end.EndedDuiZhenFragment;
import com.windoent.yugioh.ui.fragment.saishi.end.EndedPanFaFragment;
import com.windoent.yugioh.ui.fragment.saishi.end.EndedRuiShiLunFragment;
import com.windoent.yugioh.ui.fragment.saishi.end.EndedSaiGuoFragment;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.LogUtils;
import com.windoent.yugioh.utils.SharedPreUtil;
import com.windoent.yugioh.utils.TDevice;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.windoent.yugioh.widget.MessageItem;
import com.windoent.yugioh.widget.ScaleTransitionPagerTitleView;
import com.windoent.yugioh.widget.dialog.ShareQqWechatDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/20 11:46
 * desc   :比赛结束
 */
public class SaiShiEndedActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private LinearLayout mLlShare;
    private MessageItem mItemMessage;
    private MagicIndicator mMagicIndicator;
    private ViewPager mViewpager;

    private RelativeLayout mRlGuide;
    private TextView mBtnCaipanStartRuishilunGames;
    private RelativeLayout mRlSear;
    private LinearLayout mLlCaipan;
    private TextView mSearchCaipan;
    private LinearLayout mLlSelectCaipan;
    private TextView mSearchXuanshou;
    private LinearLayout mLlSearchXuanshou;
    private LinearLayout mLlSearchXuanshouLun;
    private LinearLayout mLlSearchZijiAllLun;
    private TextView mBtnGetCode;
    private TextView mBtnXuanshouTijiao;
    private ImageView mIvCaipanAvatarShenhe;
    private TextView mBtnGetCodeCaipan;
    private List<String> fragmentsTitle = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private GameManagerFragmentViewPagerAdapter pagerAdapter;
    private CommonNavigator commonNavigator;
    private String id;
    private LinearLayout mLlNodata;
    private RelativeLayout mRlContent;
    private ImageView mIvCaipanShenhe;
    private TextView mBtnGetCaipan;

    @Override
    public int getLayout() {
        return R.layout.activity_saishi_ongo_ing;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mLlShare = (LinearLayout) findViewById(R.id.ll_share);
        mItemMessage = (MessageItem) findViewById(R.id.item_message);
        mMagicIndicator = (MagicIndicator) findViewById(R.id.magicIndicator);
        mViewpager = (ViewPager) findViewById(R.id.viewpager);
        /*guide*/
        mRlGuide = (RelativeLayout) findViewById(R.id.rl_guide);
        mBtnCaipanStartRuishilunGames = (TextView) findViewById(R.id.btn_caipan_start_ruishilun_games);
        mRlSear = (RelativeLayout) findViewById(R.id.rl_sear);
        mLlCaipan = (LinearLayout) findViewById(R.id.ll_caipan);
        mSearchCaipan = (TextView) findViewById(R.id.search_caipan);
        mLlSelectCaipan = (LinearLayout) findViewById(R.id.ll_select_caipan);
        mSearchXuanshou = (TextView) findViewById(R.id.search_xuanshou);
        mLlSearchXuanshou = (LinearLayout) findViewById(R.id.ll_search_xuanshou);
        mLlSearchXuanshouLun = (LinearLayout) findViewById(R.id.ll_search_xuanshou_lun);
        mLlSearchZijiAllLun = (LinearLayout) findViewById(R.id.ll_search_ziji_all_lun);
        mBtnGetCode = (TextView) findViewById(R.id.btn_get_code);

        mBtnXuanshouTijiao = (TextView) findViewById(R.id.btn_xuanshou_tijiao);
        mIvCaipanAvatarShenhe = (ImageView) findViewById(R.id.iv_caipan_avatar_shenhe);
        mBtnGetCodeCaipan = (TextView) findViewById(R.id.btn_get_code_caipan);
        mIvCaipanShenhe = (ImageView) findViewById(R.id.iv_caipan_shenhe);
        mBtnGetCaipan = (TextView) findViewById(R.id.btn_get_caipan);

        mRlContent = (RelativeLayout) findViewById(R.id.rl_content);
        mLlNodata = (LinearLayout) findViewById(R.id.ll_nodata);
    }

    private void showNoData(boolean showNoData) {
        mLlNodata.setVisibility(showNoData ? View.VISIBLE : View.GONE);
        mRlContent.setVisibility(!showNoData ? View.VISIBLE : View.GONE);
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        EventBus.getDefault().register(mContext);
        //对阵信息
//        getDuiZHenGameDetails(id);
        showMainUI();
        //详情
        getDetailsSaiShiData(id);
    }


    //详情
    private void getDetailsSaiShiData(String id) {
        PhoneLive.getDetailsSaiShiData(getUserToken(), id, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.showText(e.getMessage());
                showNoData(true);
            }

            @Override
            public void onResponse(String response, int id) {
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            SaiShiDetailsBean json = gson.fromJson(response, SaiShiDetailsBean.class);
                            SaiShiDetailsBean.DataBean data = json.getData();
                            if (data != null) {
                                showNoData(false);
                                //magic设置
                                String role = data.getRole();
                                magic(role);
                            } else showNoData(true);
                        } else showNoData(true);
                    } catch (JSONException e) {
                        showNoData(true);
                        e.printStackTrace();
                    }
                } else {
                    showNoData(true);
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //对阵信息
    private void getDuiZHenGameDetails(String id) {
        showLoadingDialog("");
        PhoneLive.getDuiZHenGameDetails(getUserToken(), id, "all", "", "", "", new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            OnGoingDuiZhenBean json = gson.fromJson(response, OnGoingDuiZhenBean.class);
                            if (json != null) {
                                OnGoingDuiZhenBean.DataBean data = json.getData();
                                if (data != null) {
                                    boolean isStartMatchAgainst = data.isIsStartMatchAgainst();//是否开启过对阵信息
                                    boolean isNextRoundBtn = data.isIsNextRoundBtn();
                                    String roles = data.getRoles();
                                    List<OnGoingDuiZhenBean.DataBean.ResBean> res = data.getRes();
                                    if (res != null && res.size() > 0) {
                                        OnGoingDuiZhenBean.DataBean.ResBean resBean = res.get(0);
                                        if (resBean != null) {
                                            boolean is_join = resBean.isIs_join();
                                            //展示蒙层
                                            showGuide(roles, isStartMatchAgainst, is_join, resBean.getStatus_id() == 1, true, isNextRoundBtn);
                                        } else
                                            showGuide(roles, isStartMatchAgainst, false, false, true, isNextRoundBtn);
                                    } else
                                        showGuide(roles, isStartMatchAgainst, false, false, false, isNextRoundBtn);
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //展示蒙层
    private void showGuide(String roles, boolean isStartMatchAgainst, boolean is_join, boolean onGoing, boolean hasData, boolean isNextRoundBtn) {
        boolean guide_player = SharedPreUtil.getBoolean(mContext, "guide_player", false);
        boolean guide_refree = SharedPreUtil.getBoolean(mContext, "guide_refree", false);
        boolean guide_custom = SharedPreUtil.getBoolean(mContext, "guide_custom", false);
        boolean guide_youke = SharedPreUtil.getBoolean(mContext, "guide_youke", false);
        boolean notJoin = BaseMethod.notNull(roles) && roles.equals("notJoin");
        boolean pReferee = BaseMethod.notNull(roles) && roles.equals("pReferee");
        boolean player = BaseMethod.notNull(roles) && roles.equals("player");
        boolean referee = BaseMethod.notNull(roles) && roles.equals("referee");
        boolean customer = BaseMethod.notNull(roles) && roles.equals("customer");
        boolean customerReferee = BaseMethod.notNull(roles) && roles.equals("customerReferee");
        boolean pending = BaseMethod.notNull(roles) && roles.equals("pending");
        if (((pending || customer || notJoin) && !guide_youke && !isStartMatchAgainst) || (((pReferee || customerReferee) && !isNextRoundBtn))) {
            guideXuanShou(roles, false, is_join, onGoing, hasData, isNextRoundBtn);
        } else if (player && !guide_player && onGoing && !isStartMatchAgainst) {
            guideXuanShou(roles, false, is_join, true, hasData, isNextRoundBtn);
        } else if (referee && !guide_refree && !isStartMatchAgainst) {
            guideXuanShou(roles, false, is_join, onGoing, hasData, isNextRoundBtn);
        } else if ((pReferee || customerReferee) && !guide_custom && isNextRoundBtn) {
            guideXuanShou(roles, isStartMatchAgainst, is_join, onGoing, hasData, isNextRoundBtn);
        } else {
            showMainUI();
        }
    }

    //接收粘性消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(FragmentActivityMessageEvent event) {
        int message = event.getMessage();
        int type = event.getCurrentFragment();
        if (type == 1 && message == 0) {
            boolean guide_refree = SharedPreUtil.getBoolean(mContext, "guide_refree", false);
            //主裁判点击了开始瑞士轮
            if (!guide_refree)
                showGuideMainCaiPan(false);
        }
    }

    //裁判蒙层
    private void showGuideMainCaiPan(boolean hasData) {
        mBtnCaipanStartRuishilunGames.setVisibility(View.GONE);
        mViewpager.setVisibility(View.VISIBLE);
        mRlSear.setVisibility(View.VISIBLE);
        if (hasData) {
            NewbieGuide.with(SaiShiEndedActivity.this)
                    .setLabel("guide")
                    .alwaysShow(true)//总是显示，调试时可以打开
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mSearchCaipan)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_caipan_search))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mLlSelectCaipan)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_caipan_search_lun))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mIvCaipanAvatarShenhe)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_maincaipan_avatar_shenhe))
                    /*             .addGuidePage(GuidePage.newInstance()
                                         .addHighLight(mBtnGetCodeCaipan)
                                         .setLayoutRes(R.layout.view_guide_simple_duizhen_maincaipan_btn_shenhe))*/
                    .setOnGuideChangedListener(new OnGuideChangedListener() {
                        @Override
                        public void onShowed(Controller controller) {

                        }

                        @Override
                        public void onRemoved(Controller controller) {
                            showMainUI();
                            SharedPreUtil.put(mContext, "guide_custom", true);
                        }
                    })
                    .show();
        } else {
            NewbieGuide.with(SaiShiEndedActivity.this)
                    .setLabel("guide")
                    .alwaysShow(true)//总是显示，调试时可以打开
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mSearchCaipan)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_caipan_search))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mLlSelectCaipan)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_caipan_search_lun))
                    .setOnGuideChangedListener(new OnGuideChangedListener() {
                        @Override
                        public void onShowed(Controller controller) {

                        }

                        @Override
                        public void onRemoved(Controller controller) {
                            showMainUI();
                            SharedPreUtil.put(mContext, "guide_custom", true);
                        }
                    })
                    .show();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //解除注册EventBus
        EventBus.getDefault().unregister(this);
        OkHttpUtils.getInstance().cancelTag(this);
    }

    //蒙层
    private void guideXuanShou(String roles, boolean isStartMatchAgainst, boolean is_join, boolean onGoing, boolean hasData, boolean isNextRoundBtn) {
        boolean notJoin = BaseMethod.notNull(roles) && roles.equals("notJoin");
        boolean pReferee = BaseMethod.notNull(roles) && roles.equals("pReferee");
        boolean player = BaseMethod.notNull(roles) && roles.equals("player");
        boolean referee = BaseMethod.notNull(roles) && roles.equals("referee");
        boolean customer = BaseMethod.notNull(roles) && roles.equals("customer");
        boolean customerReferee = BaseMethod.notNull(roles) && roles.equals("customerReferee");
        boolean pending = BaseMethod.notNull(roles) && roles.equals("pending");
        //蒙层 用户
        showTypeUi(player, notJoin || referee || customer || pending || ((pReferee || customerReferee) && !isNextRoundBtn), (pReferee || customerReferee) && isNextRoundBtn);
        if ((notJoin || player || customer || pending) && (!is_join || !onGoing)) {//游客或未参加
            NewbieGuide.with(SaiShiEndedActivity.this)
                    .setLabel("guide")
                    .alwaysShow(true)//总是显示，调试时可以打开
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mLlSearchXuanshouLun)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_xuanshou_luns))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mSearchXuanshou)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_xuanshou_search))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mLlSearchZijiAllLun)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_xuanshou_all_luns))
                    .setOnGuideChangedListener(new OnGuideChangedListener() {
                        @Override
                        public void onShowed(Controller controller) {

                        }

                        @Override
                        public void onRemoved(Controller controller) {
                            showMainUI();
                            SharedPreUtil.put(mContext, "guide_youke", true);
                        }
                    })
                    .show();
        } else if (player && hasData) {//选手(进行中)
            NewbieGuide.with(SaiShiEndedActivity.this)
                    .setLabel("guide")
                    .alwaysShow(true)//总是显示，调试时可以打开
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mLlSearchXuanshouLun)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_xuanshou_luns))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mSearchXuanshou)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_xuanshou_search))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mLlSearchZijiAllLun)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_xuanshou_all_luns))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mBtnXuanshouTijiao)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_xuanshou_tijao))
                    .setOnGuideChangedListener(new OnGuideChangedListener() {
                        @Override
                        public void onShowed(Controller controller) {

                        }

                        @Override
                        public void onRemoved(Controller controller) {
                            showMainUI();
                            SharedPreUtil.put(mContext, "guide_player", true);
                        }
                    })
                    .show();
        } else if (pReferee || customerReferee) {//主裁判
            if (!isStartMatchAgainst) {
                mBtnCaipanStartRuishilunGames.setVisibility(View.GONE);
                mRlSear.setVisibility(View.GONE);
                mViewpager.setVisibility(View.VISIBLE);
                showGuideMainCaiPan(hasData);
            } else {
                mViewpager.setVisibility(View.GONE);
                mBtnCaipanStartRuishilunGames.setVisibility(View.VISIBLE);
                mRlSear.setVisibility(View.GONE);
                NewbieGuide.with(SaiShiEndedActivity.this)
                        .setLabel("guide")
                        .alwaysShow(true)//总是显示，调试时可以打开
                        .addGuidePage(GuidePage.newInstance()
                                .addHighLight(mBtnCaipanStartRuishilunGames)
                                .setLayoutRes(R.layout.view_guide_simple_duizhen_caipan_start))
                        .setOnGuideChangedListener(new OnGuideChangedListener() {
                            @Override
                            public void onShowed(Controller controller) {

                            }

                            @Override
                            public void onRemoved(Controller controller) {
                                mBtnCaipanStartRuishilunGames.setVisibility(View.GONE);
                                mRlSear.setVisibility(View.GONE);
                                mViewpager.setVisibility(View.VISIBLE);
                            }
                        })
                        .show();
            }
        } else if (referee && hasData) {//裁判
            NewbieGuide.with(SaiShiEndedActivity.this)
                    .setLabel("guide")
                    .alwaysShow(true)//总是显示，调试时可以打开
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mSearchCaipan)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_caipan_search))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mLlSelectCaipan)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_caipan_search_lun))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mIvCaipanShenhe)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_caipan_avatar_shenhe))
                    .addGuidePage(GuidePage.newInstance()
                            .addHighLight(mBtnGetCaipan)
                            .setLayoutRes(R.layout.view_guide_simple_duizhen_caipan_btn_shenhe))
                    .setOnGuideChangedListener(new OnGuideChangedListener() {
                        @Override
                        public void onShowed(Controller controller) {

                        }

                        @Override
                        public void onRemoved(Controller controller) {
                            showMainUI();
                            SharedPreUtil.put(mContext, "guide_refree", true);
                        }
                    })
                    .show();
        }
    }

    // 蒙层结束显示UI
    private void showMainUI() {
        mBtnCaipanStartRuishilunGames.setVisibility(View.GONE);
        mRlGuide.setVisibility(View.GONE);
        mViewpager.setVisibility(View.VISIBLE);
    }


    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mItemMessage.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, MyMsgActivity.class));
            }
        });
        mLlShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareQQWechatDialog();
            }
        });
    }

    //分享dialog
    private void shareQQWechatDialog() {
        ShareQqWechatDialog dialog = new ShareQqWechatDialog(mContext);
        dialog.setOnclickListener(new ShareQqWechatDialog.onOnclickListener() {
            @Override
            public void onClick(String name) {
                shareQQWechat(name);
            }
        });
        dialog.show();
    }

    //分享
    private void shareQQWechat(String platform) {
        OnekeyShare oks = new OnekeyShare(); //关闭sso授权 oks.disableSSOWhenAuthorize();
        //指定分享的平台，如果为空，还是会调用九宫格的平台列表界面
        if (platform != null) {
            oks.setPlatform(platform);
        }
        // title标题，微信、QQ和QQ空间等平台使用
        oks.setTitle(getString(R.string.string_share_word));
        // titleUrl QQ和QQ空间跳转链接
        oks.setTitleUrl(Constants.testWebUrl);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(getString(R.string.string_add_address_warning));
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        oks.setImageUrl("http://47.101.31.182/group1/M00/00/02/rBM8DV8ebh-ARJuGAABck3rGBcs906.png");
        // url在微信、微博，Facebook等平台中使用
        oks.setUrl(Constants.testWebUrl);
        //QQ空间
        oks.setSite(getString(R.string.app_name));
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                ToastUtils.showText(getString(R.string.string_share_sucess));
                LogUtils.d("ShareLogin", "onComplete ---->  分享成功");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                LogUtils.d("ShareLogin", "onError ---->  失败" + throwable.getStackTrace());
                LogUtils.d("ShareLogin", "onError ---->  失败" + throwable.getMessage());
                LogUtils.d("ShareSDK", "onError ---->  取消分享" + throwable.toString());
                throwable.printStackTrace();
                ToastUtils.showText(getString(R.string.string_share_fail));
            }

            @Override
            public void onCancel(Platform platform, int i) {
                LogUtils.d("ShareLogin", "onCancel ---->  分享取消");
            }
        });
        // 启动分享GUI
        oks.show(this);
    }

    private void magic(String role) {
        getInitData(role);
        setMagicIndicator();
        if (pagerAdapter == null) {
            pagerAdapter = new GameManagerFragmentViewPagerAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, mContext, fragmentsTitle, fragments);
            if (mViewpager != null)
                //The application's PagerAdapter changed the adapter's contents without calling PagerAdapter
                if (mViewpager.getAdapter() != null) {
                    pagerAdapter.notifyDataSetChanged();
                } else {
                    mViewpager.setAdapter(pagerAdapter);
                    if (fragments.size() > 1)
                        mViewpager.setCurrentItem(1);//默认显示对战
                }
        } else {
            pagerAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 底部按钮
     * "customer":"商家",
     * "customerReferee":"商家+主裁判",
     * "notJoin":"没有参加",
     * "pReferee":"主裁判",
     * "player":"选手",
     * "referee":"裁判",
     * "visitor":"游客"
     * "pending":"审核中"
     */
    private void getInitData(String role) {
        if (fragmentsTitle != null) {
            fragmentsTitle.clear();
            fragmentsTitle.add(getString(R.string.string_details));
            fragmentsTitle.add(getString(R.string.string_duizhen));
            fragmentsTitle.add(getString(R.string.string_ruishilun_rank));
            fragmentsTitle.add(getString(R.string.string_panfa));
            fragmentsTitle.add(getString(R.string.string_game_result));
        }
        if (fragments != null) {
            fragments.clear();
            fragments.add(EndedDetailsFragment.newsInstance(id));
            fragments.add(EndedDuiZhenFragment.newsInstance(id));
            fragments.add(EndedRuiShiLunFragment.newsInstance(id));
            fragments.add(EndedPanFaFragment.newsInstance(id));
            fragments.add(EndedSaiGuoFragment.newsInstance(id));
        }
    }

    private void setMagicIndicator() {
        if (commonNavigator == null) {
            commonNavigator = new CommonNavigator(mContext);
            commonNavigator.setAdjustMode(false);//均分
            CommonNavigatorAdapter adapterNavigstor = new CommonNavigatorAdapter() {
                @Override
                public int getCount() {
                    return fragmentsTitle == null ? 0 : fragmentsTitle.size();
                }

                @Override
                public IPagerTitleView getTitleView(Context context, int index) {
                    ScaleTransitionPagerTitleView titleView = new ScaleTransitionPagerTitleView(mContext);
                    titleView.setText(Html.fromHtml(fragmentsTitle.get(index)));
                    titleView.setNormalColor(Color.parseColor("#FFCCCCCC"));
                    titleView.setSelectedColor(Color.WHITE);
                    titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
                    titleView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mViewpager.setCurrentItem(index, false);
                        }
                    });
                    return titleView;
                }

                @Override
                public IPagerIndicator getIndicator(Context context) {
                    LinePagerIndicator indicator = new LinePagerIndicator(mContext);
                    indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                    indicator.setLineHeight(TDevice.dp2px(mContext, 5));
                    indicator.setLineWidth(TDevice.dp2px(mContext, 30));
                    indicator.setRoundRadius(TDevice.dp2px(mContext, 6));
                    indicator.setStartInterpolator(new AccelerateInterpolator());
                    indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                    indicator.setColors(Color.parseColor("#FFEC4E4E"));
                    return indicator;
                }
            };
            commonNavigator.setAdapter(adapterNavigstor);

            if (mMagicIndicator != null)
                mMagicIndicator.setNavigator(commonNavigator);
            if (mMagicIndicator != null && mViewpager != null)
                ViewPagerHelper.bind(mMagicIndicator, mViewpager);
        } else {
            commonNavigator.notifyDataSetChanged();
        }
    }

    //蒙层ui
    private void showTypeUi(boolean xuanshou, boolean caipan, boolean main) {
        mBtnCaipanStartRuishilunGames.setVisibility(main ? View.VISIBLE : View.GONE);
        mRlSear.setVisibility(!caipan ? View.VISIBLE : View.GONE);
        mLlCaipan.setVisibility(caipan || main ? View.VISIBLE : View.GONE);
        mSearchXuanshou.setVisibility(xuanshou ? View.VISIBLE : View.GONE);
        mLlSearchXuanshou.setVisibility(xuanshou ? View.VISIBLE : View.GONE);
        mBtnGetCode.setVisibility(main ? View.VISIBLE : View.GONE);
        mBtnXuanshouTijiao.setVisibility(xuanshou ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "赛事比赛中界面");
        if (mItemMessage != null) {
            mItemMessage.setTvTitle(MyApplication.getInstance().getMsgNumber());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "赛事比赛中界面");
    }
}
